package com.ccloud.blog.modules.blog.controller;


import com.ccloud.blog.modules.blog.domain.BlogType;
import com.ccloud.blog.modules.blog.service.IBlogService;
import com.ccloud.blog.modules.blog.service.IBlogTypeService;
import com.ccloud.common.core.model.LoginVal;
import com.ccloud.common.core.reqres.response.R;
import com.ccloud.common.core.utils.OauthUtils;
import com.ccloud.common.core.utils.StringUtils;
import com.ccloud.common.core.utils.ValueUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project BlogController
 * @Title BlogController.java
 * @Description blog 控制器
 * @Package com.echo.blog.business.blog.controller
 * @date 2019/9/5 18:03
 * @Copyright: 上海顺益信息科技有限公司 All rights reserved.
 */
@RestController
@Slf4j
@RequestMapping("/blogType")
public class BlogTypeController {

    @Resource
    private IBlogTypeService blogTypeService;

    @Resource
    private IBlogService blogService;

    @RequestMapping(value = "/list")
    public R getBlogList() {
        List<Map<String, Object>> list = blogTypeService.getBloyTypeList();
        return R.ok(list, "成功");
    }


    @RequestMapping(value = "/typeList")
    public R getTypeList() {
        LoginVal loginVal = OauthUtils.getCurrentUser();
        Integer userId = loginVal.getIntegerUserId();
        List<BlogType> list = blogTypeService.getBlogTypeList(userId);
        return R.ok(list, "获取博客类型列表成功");
    }

    @RequestMapping(value = "/typeIndex")
    public R getTypeIndex(String blogType) {
        LoginVal loginVal = OauthUtils.getCurrentUser();
        Integer userId = loginVal.getIntegerUserId();
        if (StringUtils.isBlank(blogType)) {
            return R.fail("博客类型id为空");
        }
        BlogType data = blogTypeService.getBlogTypeIndex(userId, ValueUtils.parseInteger(blogType));
        return R.ok(data, "获取博客类型索引成功");
    }

    @RequestMapping(value = "/add")
    public R addBlogType(String sortName) {
        if (StringUtils.isBlank(sortName)) {
            return R.fail("参数中，sortName为空");
        }
        LoginVal loginVal = OauthUtils.getCurrentUser();
        Integer userId = loginVal.getIntegerUserId();
        //重名校验
        if (blogTypeService.validRepeat(sortName, userId)) {
            return R.fail("该分类已经存在了←.←");
        }
        boolean result = blogTypeService.insertBlogType(sortName, userId);
        if (result) {
            return R.ok(null, "添加分类成功");
        }
        return R.fail("添加分类失败");
    }


    @RequestMapping(value = "/edit")
    public R edit(BlogType blogType) {
        //重名校验
        if (blogTypeService.validRepeat(blogType.getName(), blogType.getId())) {
            return R.fail("该分类已经存在了←.←");
        }
        boolean result = blogTypeService.updateById(blogType);
        if (result) {
            return R.ok(null, "分类名称修改成功");
        } else {
            return R.fail(null, "分类修改失败");
        }
    }

    @RequestMapping(value = "/delete")
    public R delete(BlogType blogType) {
        //移除基础博客类型
        boolean result = blogTypeService.removeById(blogType.getId());
        //移除关联下的所有博客
        boolean flag = blogService.removeByBlogTypeId(blogType.getId());
        if (result && flag) {
            return R.ok(null, "分类名称删除成功");
        } else {
            return R.fail("分类删除失败");
        }
    }


}
